package com.getpebble.android.main.sections.settings.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.getpebble.android.R;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.framework.fragment.PblBaseFragment;
import com.getpebble.android.onboarding.activity.OnboardingActivity;

public class LanguageSelectionCompleteFragment extends PblBaseFragment {
    private static final String TAG = LanguageSelectionCompleteFragment.class.getSimpleName();
    private TextView mInstallCompleteDescription;
    private String mInstallLanguageName;
    private boolean mIsOnboarding = false;

    public int getLayoutId() {
        return R.layout.fragment_language_install_complete;
    }

    public void init(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final Activity activity = getActivity();
        if (activity == null) {
            Trace.warning(TAG, "init() activity is null, not initializing");
            return;
        }
        this.mIsOnboarding = activity instanceof OnboardingActivity;
        if (!this.mIsOnboarding) {
            container.findViewById(R.id.onboarding_banner).setVisibility(8);
        }
        ((Button) container.findViewById(R.id.language_setting_complete_button)).setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (LanguageSelectionCompleteFragment.this.getActivity() == null) {
                    Trace.debug(LanguageSelectionCompleteFragment.TAG, "Null activity in onClick(); dropping");
                } else if (LanguageSelectionCompleteFragment.this.mIsOnboarding) {
                    ((OnboardingActivity) activity).goToNextScreen();
                } else {
                    activity.finish();
                }
            }
        });
        this.mInstallCompleteDescription = (TextView) container.findViewById(R.id.onboarding_language_install_complete_subtitle);
    }

    public void onResume() {
        super.onResume();
        Activity activity = getActivity();
        if (activity == null) {
            Trace.error(TAG, "Activity is null; short-circuiting onResume");
        } else if (handleArguments()) {
            Trace.debug(TAG, String.format("Set up language install fragment; language=<%s>", new Object[]{this.mInstallLanguageName}));
            this.mInstallCompleteDescription.setText(String.format(activity.getResources().getString(R.string.language_set_successfully_description), new Object[]{this.mInstallLanguageName}));
        } else {
            Trace.error(TAG, "Failed to set up fragment; finishing fragment");
            if (this.mIsOnboarding) {
                ((OnboardingActivity) activity).switchScreenFragment(null);
                return;
            }
            Trace.error(TAG, "Finishing activity");
            activity.finish();
        }
    }

    private boolean handleArguments() {
        Bundle args = getArguments();
        if (args == null) {
            Trace.error(TAG, "Cannot install language; no arguments provided to fragment");
            return false;
        } else if (args.containsKey("extra_install_language_name")) {
            this.mInstallLanguageName = args.getString("extra_install_language_name");
            return true;
        } else {
            Trace.error(TAG, "Cannot install language; no user-facing language name provided");
            return false;
        }
    }
}
